var isInit = true;
$.get('selectGoodsByCodePrefix', function (res) {
    $('#airborne .type_code').html('')
    $('#airborne .type_code').append(template('options', res))
    $('.hidenCodes').val(JSON.stringify(res.data));
    loadChildData(res.data[0].goodsCode); 
    searchData(1)
})
//请求图表数据
$.get('searchOperateImage', function (res) {
    initCharts(res)
})

//增加级联
$('.type_code').change(function()
{
	loadChildData($(this).val())
});

//加载子节点方法
function loadChildData(code)
{
	var parent_code=code;
	var codes= eval($('.hidenCodes').val());
	$('.type_code_child').html('');
	$('.type_code_child').append("<option value='nion'></option>");
	$.each(codes,function(i,e)
	{
		if(parent_code==e.goodsCode)
		{
			var options=e.series.split(' ');
			$.each(options,function(i,e)
		    {
			   $('.type_code_child').append("<option value='"+e+"'>"+e+"</option>");
			});
			return true;  
		
		}
	});
}
//初始化表格数据
function searchData(page) {
    var start_time = $('.start_time').val()
    var end_time = $('.end_time').val()
    var type_code = $('.type_code option:selected').val()
    var type_code_child = $('.type_code_child option:selected').val()
    $.get('searchOperateInfo', {
        page: page,
        size: 10,
        start_time: start_time,
        end_time: end_time,
        type_code: type_code,
        type_code_child:type_code_child
    }, function (res) {
        console.log(res)
        if (res.status != 1) {
            alert(res.msg)
            return
        }
        $('#airborne .bodyContent').html('')
        $('#airborne .bodyContent').append(template('bodyContent', res.data))
        if (res.data.total_count) {
            $('.dataCount>span').html('10/' + res.data.total_count)
        }
        if (isInit) {
            isInit = false
            setPage(res.data.total_count)
        }
    })
}
//搜索按钮
function  search() {
    isInit=true;
    searchData(1)
}
//设置翻页插件
function setPage(size) {
    $('#page').pagination({
        pageCount: (function () {
            if (size % 10 == 0) {
                return size / 10
            }
            return Math.floor(size / 10 + 1)
        })(),
        jump: true,
        coping: true,
        homePage: '首页',
        endPage: '尾页',
        prevContent: '上页',
        nextContent: '下页',
        callback: function (api) {
            searchData(api.getCurrent())
        }
    });
}

//空投比豆用户数.次数
function initCharts(obj) {
    var title = [], userNumber = [], bibenCount = [];
    for (var i = 0; i < obj.data.length; i++) {
        title.push(obj.data[i].image_name)
        userNumber.push(obj.data[i].image_user_count)
        bibenCount.push(obj.data[i].image_biben_count)
    }
    var userNumberChart = echarts.init(document.querySelector('.userNumber'));
    var airborneNumberChart = echarts.init(document.querySelector('.airborneNumber'));
// 指定图表的配置项和数据
    var option = {
        title: {
            text: "大叔社区问卷各期发放比豆数",
            padding: [0, 0, 0, 50],
        },
        legend: {
            data: ["比豆数"],
            x: '48%',
            y: '90%',
        },
        xAxis: {
            type: "category",
            data: title
        },
        color: ['#7cb5ec'],
        yAxis: {
            type: "value"
        },
        tooltip: {},
        series: [{
            name: "比豆数",
            data: bibenCount,
            type: "bar",
            itemStyle: {
                normal: {
                    label: {
                        show: true
                    }
                }
            }
        }]
    }

    var option2 = {
        title: {
            text: "大叔社区问卷各期发放用户数",
            padding: [0, 0, 0, 50],
        },
        legend: {
            data: ["用户数"],
            x: '48%',
            y: '90%',
        },
        xAxis: {
            type: "category",
            data: title
        },
        color: ['#434348'],
        yAxis: {
            type: "value"
        },
        tooltip: {},
        series: [{
            name: "用户数",
            data: userNumber,
            type: "bar",
            itemStyle: {
                normal: {
                    label: {
                        show: true
                    }
                }
            }
        }]
    }
// 使用刚指定的配置项和数据显示图表。
    userNumberChart.setOption(option);
    airborneNumberChart.setOption(option2);
}

function download() {
    var start_time = $('.start_time').val()
    var end_time = $('.end_time').val()
    if (start_time == '' || end_time == '') {
        alert('请选择正确的日期')
        return
    }
    var type_code = $('.type_code option:selected').val()
    var type_code_child = $('.type_code_child option:selected').val()
    var url = 'downloadOperateReport?start_time=' + start_time + '&end_time=' + end_time + '&type_code=' + type_code +"&type_code_child="+type_code_child
    window.open(url)
}
